package com.lagou.handler;

import com.lagou.common.RpcRequest;
import com.lagou.service.SpringContent;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.lang.reflect.Method;

public class UserServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof RpcRequest){
            RpcRequest rpcRequest = (RpcRequest)msg;
            String className = rpcRequest.getClassName();
            Class<?> aClass1 = Class.forName(className);
            Object bean = SpringContent.getBean(aClass1);
            Method method = aClass1.getMethod(rpcRequest.getMethodName(), rpcRequest.getParameterTypes());
            method.invoke(bean, rpcRequest.getParameters());
            ctx.writeAndFlush("success");
        }
    }
}
